#include "mutex_interface.h"
#include <pthread.h>
#include "../debug.h"


static pthread_mutex_t mtx[MUTEX_IDX_QTY];


int MutexInf_Init(mutexIdx_t idx)
{
    int ret;

    if(idx >= MUTEX_IDX_QTY)
    {
        PRINT_WARN("idx[%d]", idx);
        return -1;
    }

    if((ret = pthread_mutex_init(&mtx[idx], NULL)) != 0)
    {
        PRINT_WARN("ret[%d]", ret);
    }

    return ret;
}


int MutexInf_Destroy(mutexIdx_t idx)
{
    int ret;

    if(idx >= MUTEX_IDX_QTY)
    {
        PRINT_WARN("idx[%d]", idx);
        return -1;
    }

    if((ret = pthread_mutex_destroy(&mtx[idx])) != 0)
    {
        PRINT_WARN("ret[%d]", ret);
    }

    return ret;
}


int MutexInf_Lock(mutexIdx_t idx)
{
    int ret;

    if(idx >= MUTEX_IDX_QTY)
    {
        PRINT_WARN("idx[%d]", idx);
        return -1;
    }

    if((ret = pthread_mutex_lock(&mtx[idx])) != 0)
    {
        PRINT_WARN("ret[%d]", ret);
    }

    return ret;
}


int MutexInf_Unlock(mutexIdx_t idx)
{
    int ret;

    if(idx >= MUTEX_IDX_QTY)
    {
        PRINT_WARN("idx[%d]", idx);
        return -1;
    }

    if((ret = pthread_mutex_unlock(&mtx[idx])) != 0)
    {
        PRINT_WARN("ret[%d]", ret);
    }

    return ret;
}
